import com.hailiao.bdproto2p1.BDProto2p1
import com.hailiao.bdproto2p1guowang.BDProto2p1Guowang
import com.hailiao.hlbdcore.HLBDCore
import com.hailiao.hlbdcore.annotation.HLBDReceive
import com.hailiao.hlbdcore.annotation.HLBDReceiveRaw
import com.hailiao.hlbdcore.annotation.HLBDSendRaw
import com.hailiao.hlbdcore.base.BaseBDProto
import com.hailiao.hlbdcore.base.BaseOutBean
import com.hailiao.hlbdcore.base.HLBDClient
import com.hailiao.hlbdcore.constant.Core

class MyApp {

    /** 监听器 */
    private var mReceiveListener: ((bean: BaseOutBean) -> Unit)? = null
    /** 监听器 */
    private var mReceiveRowListener: ((str: String, raw: ByteArray) -> Unit)? = null

    /**
     * 初始化
     * @param listener Function1<[@kotlin.ParameterName] BaseOutBean, Unit>?
     * @param rowListener Function1<[@kotlin.ParameterName] String, Unit>?
     */
    fun init(listener: ((bean: BaseOutBean) -> Unit)? = null, rowListener: ((str: String, raw: ByteArray) -> Unit)? = null) {
        mReceiveListener = listener
        mReceiveRowListener = rowListener
        // 初始化协议
        BDProto2p1.init()
        BDProto2p1Guowang.init()

        // 监听北斗消息
        HLBDCore.register(this)
    }

    @HLBDReceive
    fun onReceive(client: HLBDClient, device: String, proto: BaseBDProto, bean: BaseOutBean) {
        mReceiveListener?.invoke(bean)
    }

    @HLBDReceiveRaw
    fun onRawReceive(client: HLBDClient, device: String, raw: ByteArray) {
        mReceiveRowListener?.invoke(raw.toString(Core.GBK_CHARSET), raw)
    }

    @HLBDSendRaw
    fun onRawSend(client: HLBDClient, device: String, raw: ByteArray) {
        mReceiveRowListener?.invoke(raw.toString(Core.GBK_CHARSET), raw)
    }
}